3 // how many nodes are beneath this one?
6 // array of pointers to children
7 trie_node
*child_list
[ALPH_SIZE
];
13 trie_node::trie_node() {
15 // there is nothing below us...
18 // all child pointers start out pointing nowhere...
19 for(int index
= 0; index
< ALPHABET_SIZE
; index
++)
20 child_list
[index
] = NULL
;
31 Trie(int level
, int chars
); // create it
32 ~Trie(); // destroy trie nodes
33 int node_count
; // the number of nodes in whole trie
34 void AddToTrie(const string \
&s
); // add string to trie
40 void recursive_delete (trie_node
* t
); // recursive delete helper function
46 int node_count
; // number of nodes in the trie
47 trie_node
* Root
; // the root of the trie
62 recursive_delete(myRoot
);
66 void Trie::recursive_delete(trie_node
* t
) {
71 // delete all children
72 for(index
= 0; index
< ALPHABET_SIZE
; index
++)
73 recursive_delete(t
->child_list
[index
]);
83 int Trie::node_count() {
91 void Trie::AddToTrie(const string \
&s
) {
96 // there is one more string stored somewhere under the root...
100 // loop over the length of the string to add and traverse the trie...
101 for(lcv
=0; lcv
< s
.length(); lcv
++) {
103 index
= s
[lcv
]; // the character in s we are processing...
105 // is there a child node for this character?
106 if (t
->child_list
[index
] == NULL
) {
109 t
->child_list
[index
] = new trie_node
;
113 // there is another string under this node...
114 t
->child_list
[index
]->count
++;
116 // move to it this node... and loop
117 t
= t
->child_list
[index
];
125 while (cin
>> N
>> M
&& (N
|| M
)){
127 unsigned long long i
;
132 str
= str
.substr(0, str
.size() - 1); //to remove the *
138 trie_node
*r
= t
.Root
; //I must keep the root of the Trie because I will change it later.
140 str
= str
.substr(0, str
.size() - 1);
141 unsigned long long j
= 0;
142 for(j
=0; j
<str
.size(); j
++)
144 t
.Root
= t
.Root
->child_list
[str
[j
] - '0'];
146 unsigned long long res
= t
.Find(str
, 1);
148 t
.Root
= r
; //go back to the original root.